package com.box.service.impl;

import com.box.entity.DbTemplates;
import com.box.entity.DbTemplatesExample;
import com.box.mapper.DbTemplatesMapper;
import com.box.service.TemplateService;
import com.box.utils.LayuiResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: MyBox
 * @ClassName TemplateServiceImpl
 * @description:
 * @author: tyf19970214
 * @create: 2020-02-04 23:11
 * @Version 1.0
 **/
@Service
public class TemplateServiceImpl implements TemplateService {

    private Logger logger=Logger.getLogger(TemplateServiceImpl.class);

    @Autowired
    private DbTemplatesMapper dbTemplatesMapper;

    @Override
    public LayuiResult getDataList(Integer page,Integer limit) {
        try{
            PageHelper.startPage(page,limit);
            DbTemplatesExample example=new DbTemplatesExample();
            example.setOrderByClause("t_updateTime desc");
            List<DbTemplates> templatesList = dbTemplatesMapper.selectByExampleWithBLOBs(example);
            logger.debug("获取的列表:"+templatesList);
            PageInfo<DbTemplates> pageInfo=new PageInfo<DbTemplates>(templatesList);
            LayuiResult result=new LayuiResult();
            result.setCode(0);
            result.setMsg("获取成功");
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
            return result;
        }catch (Exception e){
            LayuiResult result=new LayuiResult();
            result.setCode(500);
            result.setMsg("获取失败");
            result.setCount(0l);
            result.setData(null);
            return result;
        }
    }

    @Override
    public Integer deleteById(Integer id) {
        return dbTemplatesMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteByAll(String TemplateStr) {
        String[] ids = TemplateStr.split(",");
        Integer n=0;
        if(ids.length>0 && ids!=null){
            for (String id:ids
                 ) {
           n=     dbTemplatesMapper.deleteByPrimaryKey(Integer.parseInt(id));


            }
        }
        logger.debug("批量删除"+n);
        return n;
    }

    @Override
    public Integer insertTemplate(DbTemplates templates) {
        templates.settCreatetime(new Date());
        templates.settUpdatetime(new Date());
     Integer n=   dbTemplatesMapper.insert(templates);
     logger.debug("添加模板:"+n);
        return n;
    }


    @Override
    public DbTemplates getOneById(Integer id) {
        DbTemplatesExample example=new DbTemplatesExample();
        DbTemplatesExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);



        return  dbTemplatesMapper.selectByExampleWithBLOBs(example).get(0);

    }


    @Override
    public Integer updateTemplate(DbTemplates templates) {

        templates.settUpdatetime(new Date());
        templates.settCreatetime(new Date());
        return  dbTemplatesMapper.updateByPrimaryKey(templates);

    }

    @Override
    public List<DbTemplates> getListByCreateTime() {

        DbTemplatesExample example=new DbTemplatesExample();
        example.setOrderByClause("t_createTime desc");
        return dbTemplatesMapper.selectByExampleWithBLOBs(example);

    }
}
